﻿package morn.core.managers
{
    import __AS3__.vec.*;
    import com.sh.game.global.*;
    import com.sh.game.util.*;
    import flash.display.*;
    import flash.events.*;
    import flash.net.*;
    import flash.system.*;
    import flash.utils.*;
    import morn.core.handlers.*;
    import morn.core.utils.*;

    public class ResLoader extends Object
    {
        private var _loader:Loader;
        private var _urlLoader:URLLoader;
        private var _urlRequest:URLRequest;
        private var _loaderContext:LoaderContext;
        private var _url:String;
        private var _type:int;
        private var _complete:Handler;
        private var _progress:Handler;
        private var _isCache:Boolean;
        private var _isLoading:Boolean;
        private var _loaded:Number;
        private var _lastLoaded:Number;
        private var _retry:int;
        public static const SWF:uint = 0;
        public static const MSWF:uint = 7;
        public static const BMD:uint = 1;
        public static const AMF:uint = 2;
        public static const TXT:uint = 3;
        public static const DB:uint = 4;
        public static const BYTE:uint = 5;
        public static const BM:uint = 6;
        private static var _loadedMap:Object = {};
        private static var _bytesDic:Dictionary = new Dictionary();
        private static var _bytesSize:int = 0;
        private static var _bytesUrls:Vector.<String> = new Vector.<String>;

        public function ResLoader()
        {
            this._loader = new Loader();
            this._urlLoader = new URLLoader();
            this._urlRequest = new URLRequest();
            this._loaderContext = new LoaderContext(false, ApplicationDomain.currentDomain);
            this._loader.contentLoaderInfo.addEventListener(Event.COMPLETE, this.onComplete);
            this._loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, this.onError);
            this._loader.contentLoaderInfo.addEventListener(HTTPStatusEvent.HTTP_STATUS, this.onStatus);
            this._urlLoader.addEventListener(ProgressEvent.PROGRESS, this.onProgress);
            this._urlLoader.addEventListener(Event.COMPLETE, this.onComplete);
            this._urlLoader.addEventListener(IOErrorEvent.IO_ERROR, this.onError);
            this._urlLoader.addEventListener(HTTPStatusEvent.HTTP_STATUS, this.onStatus);
            this._urlLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, this.onError);
            return;
        }// end function

        public function tryToCloseLoad() : void
        {
            try
            {
                App.timer.clearTimer(this.checkLoad);
                this._isLoading = false;
                this._loader.unloadAndStop(false);
                this._urlLoader.close();
            }
            catch (e:Error)
            {
                Logger.log("取消加载失败");
            }
            return;
        }// end function

        private function doLoad() : void
        {
            var _loc_1:ByteArray = null;
            var _loc_2:int = 0;
            this._isLoading = true;
            if (this._retry > 0)
            {
                this._urlRequest.url = this._url + "&r=" + this._retry;
            }
            else
            {
                this._urlRequest.url = this._url;
            }
            if (this._type == SWF || this._type == BMD || this._type == BM)
            {
                _loc_1 = _bytesDic[this._url];
                if (_loc_1)
                {
                    this._urlLoader.data = null;
                    this.onBytesLoaded(_loc_1);
                    _loc_2 = _bytesUrls.indexOf(this._url);
                    if (_loc_2 != -1)
                    {
                        _bytesUrls.splice(_loc_2, 1);
                    }
                    _bytesUrls.push(this._url);
                    return;
                }
                this._urlLoader.dataFormat = URLLoaderDataFormat.BINARY;
                this._urlLoader.load(this._urlRequest);
                return;
            }
            else if (this._type == MSWF || this._type == AMF || this._type == DB || this._type == BYTE)
            {
                this._urlLoader.dataFormat = URLLoaderDataFormat.BINARY;
                this._urlLoader.load(this._urlRequest);
            }
            else if (this._type == BM)
            {
                this._loader.load(this._urlRequest);
            }
            else if (this._type == TXT)
            {
                this._urlLoader.dataFormat = URLLoaderDataFormat.TEXT;
                this._urlLoader.load(this._urlRequest);
                return;
            }
            return;
        }// end function

        private function onStatus(event:HTTPStatusEvent) : void
        {
            return;
        }// end function

        private function onError(event:Event) : void
        {
            this.endLoad(null, 2);
            return;
        }// end function

        private function onProgress(event:ProgressEvent) : void
        {
            var _loc_2:Number = NaN;
            if (this._progress != null)
            {
                _loc_2 = event.bytesLoaded / event.bytesTotal;
                this._progress.executeWith([_loc_2]);
            }
            this._loaded = event.bytesLoaded;
            return;
        }// end function

        private function onBytesLoaded(param1:ByteArray) : void
        {
            this._loader.loadBytes(param1, this._loaderContext);
            return;
        }// end function

        private function onComplete(event:Event) : void
        {
            var _loc_3:ByteArray = null;
            var _loc_4:String = null;
            var _loc_5:ByteArray = null;
            var _loc_6:int = 0;
            var _loc_7:int = 0;
            var _loc_8:ByteArray = null;
            var _loc_2:* = null;
            if (this._type == MSWF || this._type == SWF || this._type == BMD || this._type == BM)
            {
                if (this._urlLoader.data != null)
                {
                    _loc_3 = this._urlLoader.data;
                    if (this._type == MSWF)
                    {
                        _loc_3.position = 0;
                        _loc_4 = Config.mswfkey;
                        _loc_5 = new ByteArray();
                        _loc_5.writeMultiByte(_loc_4, "utf8");
                        _loc_6 = _loc_3.length / 2;
                        _loc_3.position = _loc_6;
                        _loc_7 = 0;
                        while (_loc_7 < _loc_5.length)
                        {
                            
                            _loc_3.writeByte(_loc_3[_loc_7 + _loc_6] ^ _loc_5[_loc_7]);
                            _loc_7++;
                        }
                    }
                    this._urlLoader.data = null;
                    this.onBytesLoaded(_loc_3);
                    return;
                }
                if (this._type == MSWF)
                {
                    this._loader.unloadAndStop(false);
                    _loc_2 = 1;
                }
                else if (this._type == SWF)
                {
                    this._loader.unloadAndStop(false);
                    _loc_2 = 1;
                }
                else if (this._type == BMD)
                {
                    _loc_2 = Bitmap(this._loader.content).bitmapData;
                    this._loader.unloadAndStop(false);
                }
                else
                {
                    _loc_2 = Bitmap(this._loader.content);
                    this._loader.unloadAndStop(false);
                }
            }
            else if (this._type == AMF)
            {
                _loc_2 = ObjectUtils.readAMF(this._urlLoader.data);
            }
            else if (this._type == DB)
            {
                _loc_8 = this._urlLoader.data as ByteArray;
                _loc_8.uncompress();
                _loc_2 = _loc_8.readObject();
            }
            else if (this._type == BYTE)
            {
                _loc_2 = this._urlLoader.data as ByteArray;
            }
            else if (this._type == TXT)
            {
                _loc_2 = this._urlLoader.data;
            }
            if (!this._isLoading)
            {
                this._loader.unloadAndStop(false);
                this.endLoad(null, 1);
                return;
            }
            if (this._isCache)
            {
                _loadedMap[this._url] = _loc_2;
            }
            this.endLoad(_loc_2);
            return;
        }// end function

        private function endLoad(param1, param2:int = 0) : void
        {
            var _loc_3:Handler = null;
            App.timer.clearTimer(this.checkLoad);
            this._isLoading = false;
            this._progress = null;
            if (this._complete != null)
            {
                _loc_3 = this._complete;
                this._complete = null;
                _loc_3.executeWith([param1, param2]);
            }
            return;
        }// end function

        public function load(param1:String, param2:int, param3:Handler, param4:Handler, param5:Boolean = true, param6:int = 8000, param7:int = 0) : void
        {
            if (this._isLoading)
            {
                this.tryToCloseLoad();
            }
            this._retry = param7;
            this._url = param1;
            this._type = param2;
            this._complete = param3;
            this._progress = param4;
            this._isCache = param5;
            var _loc_8:* = getResLoaded(param1);
            if (getResLoaded(param1) != null)
            {
                return this.endLoad(_loc_8);
            }
            var _loc_9:int = 0;
            this._lastLoaded = 0;
            this._loaded = _loc_9;
            App.timer.doLoop(param6, this.checkLoad);
            this.doLoad();
            return;
        }// end function

        private function checkLoad() : void
        {
            if (this._loaded - this._lastLoaded < 1024)
            {
                this.tryToCloseLoad();
                this.endLoad(null, 1);
            }
            else
            {
                this._lastLoaded = this._loaded;
            }
            return;
        }// end function

        public function get url() : String
        {
            return this._url;
        }// end function

        public static function hadCache(param1:String) : Boolean
        {
            return false;
        }// end function

        public static function getResLoaded(param1:String)
        {
            return _loadedMap[param1];
        }// end function

        public static function setResLoaded(param1:String, param2) : void
        {
            _loadedMap[param1] = param2;
            return;
        }// end function

        public static function clearResLoaded(param1:String) : void
        {
            var _loc_2:* = _loadedMap[param1];
            if (_loc_2 is BitmapData)
            {
                BitmapData(_loc_2).dispose();
            }
            else if (_loc_2 is Bitmap)
            {
                Bitmap(_loc_2).bitmapData.dispose();
            }
            delete _loadedMap[param1];
            return;
        }// end function

    }
}
